package com.itboye.ihomebank.net;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;

import com.android.volley.NoConnectionError;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.itboye.ihomebank.base.ICompleteListener;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.net.UnknownHostException;

public class BaseErrorListener implements XErrorListener {
	private ICompleteListener listener;

	public BaseErrorListener(ICompleteListener listener){

		this.listener = listener;
	}

	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@Override
	public void onErrorResponse(Exception exception, String code, String msg) {

		if(listener != null){

			if(msg == null || msg.isEmpty()){
				if(exception != null){
					msg = exception.getMessage();
				}else{
					msg = "发生未知错误!";
				}
			}
			ResultEntity result = null;
			//服务器报错 切换到islock
//			SPUtils.put(MyApplcation.ctx,null,SPContants.ISLOCK,false);
			if (code.equals("1111")){ //sid 失效
				ServerError jsonException=new ServerError();
				//msg = "服务器返回错误!"+error.getMessage()+"："+code;
				msg = "无法连接到服务器("+code+")";
				result=new ResultEntity(code,msg, jsonException);
				listener.failure(result);
			}else if(exception instanceof ServerError||exception instanceof TimeoutError){
				//服务器报错 切换到islock
//				SPUtils.put(MyApplcation.ctx,null,SPContants.ISLOCK,true);

				if ("".equals(SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,""))){ //锁服务器报错
					if (exception instanceof ServerError){
						ServerError jsonException=(ServerError) exception;
						//msg = "服务器返回错误!"+error.getMessage()+"："+code;
						msg = "无法连接到服务器("+code+")";
						result=new ResultEntity(code,msg, jsonException);
					}else {
						TimeoutError jsonException=(TimeoutError) exception;
						//msg = "服务器返回错误!"+error.getMessage()+"："+code;
						msg = "网络连接超时("+code+")，请检查您的网络或稍后重试";
						result=new ResultEntity(code,msg, jsonException);
					}
				}else {
					VolleyError jsonException=(VolleyError) exception;
					//msg = "服务器返回错误!"+error.getMessage()+"："+code;
					msg = "网络连接超时("+code+")，请检查您的网络或稍后重试";
					result=new ResultEntity(code,msg, jsonException);
				}

				listener.failure(result);
			} else if(exception instanceof NoConnectionError){
				NoConnectionError jsonException=(NoConnectionError) exception;
				String error=exception.getLocalizedMessage();
				msg = "当前网络不可用，请检查手机网络设置";
				result=new ResultEntity(code,msg, jsonException);
				listener.failure(result);
			}
			else if(exception instanceof VolleyError){
				VolleyError jsonException=(VolleyError) exception;
				String error=jsonException.getLocalizedMessage();
//				msg = "当前网络不可用，请检查手机网络设置";
				result=new ResultEntity(code,msg, jsonException);
				listener.failure(result);
			}else {
				result=new ResultEntity(code,msg, exception);
				listener.failure(result);
			}

		}				
	}
}

